<?php
class kwitansipembeliancontroller extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('masterbank_model');
		$this->load->model('delaypembelian_model');
		$this->load->model('transaksibankpembelian_model');
		$this->load->model('kwitansimutasicash_model');
		$this->load->model('kwitansimutasibank_model');
		$this->load->model('kwitansimutasigiro_model');
		$this->load->model('kwitansipembelian_model');
		$this->load->model('stock_model');
		$this->load->model('mstraccount_model');
		$this->load->library('session');
		$this->load->library('pagination_editable');
		if(($this->session->userdata('fullname') == '') || ($this->session->userdata('fullname') == NULL)){
			redirect('logincontroller');
		}
	}
	
	function index(){    
		/*$config = array();
		$config['per_page'] = 10;
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['curr_class'] = 'active';
		$config['full_tag_open'] = '<ul id="pagination-flickr">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['next_link'] = 'Next &gt;&gt;';
		$config['prev_link'] = '&lt;&lt; Previous';
		$config['next_tag_open'] = '<li class="next">';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li class="previous-off">';
		$config['prev_tag_close'] = '</li>';
		
		$config["uri_segment"] = 3;
		$config["base_url"] = site_url('kwitansipembeliancontroller/index');
		$config['total_rows'] = $this->kwitansipembelian_model->countGetList();
		
		$this->pagination_editable->initialize($config);
	
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;*/
		           
		if (isset($_POST['btnview'])) {
            if (isset($_POST['nofaktur'])) 
			{
				$nofaktur = $_POST['nofaktur'];
				$this->kwitansipembelian_model->setNoFaktur($nofaktur);
				$dataprofile = array(
					'page_title' => 'Finance > kwitansi Pembelian',
					'name' => $this->session->userdata('fullname'),
					'mstrfaktur' => $this->kwitansipembelian_model->ambilnofaktur(),
					'masterbank' => $this->masterbank_model->getListbank(),
					'det_barang' => $this->kwitansipembelian_model->getnofakturlike(),
					'bankcombo' => $this->bankCombobox(),
					//'links' => $this->pagination_editable->create_links(),
					'message'=>''
				);  
            }
        } 
		else 
		{			
			$dataprofile = array(
				'page_title' => 'Finance > kwitansi Pembelian',
				'name' => $this->session->userdata('fullname'),
				'mstrfaktur' => $this->kwitansipembelian_model->ambilnofaktur(),
				'masterbank' => $this->masterbank_model->getListbank(),
				'det_barang' => $this->kwitansipembelian_model->getList(),
				'bankcombo' => $this->bankCombobox(),
				//'links' => $this->pagination_editable->create_links(),
				'message'=>''
			);		
		}
		$content = array(
			'content' => 'finance/kwitansi/pembelian/kwitansipembelian'
		);
		$this->template->load('templates/default',$content,$dataprofile);
	}
	
	function bankCombobox(){
		$data = $this->masterbank_model->getListbank();
		$mycombo = '';
		$mycombo = '<select id="bank" name="bank" onchange="javascript:getBankDetail()">';
		$mycombo .= '<option value="0">Pilih Bank</option>';
		foreach($data as $bank){			
			$mycombo .= '<option value="'.$bank->KodeBank.'">'.$bank->Bankname.'</option>';
		}
		$mycombo .= '</select>';
		return $mycombo;
	}
	
	function getBankDetail(){
		$bankid = $_POST['bankid'];
		$this->masterbank_model->setKodeBank($bankid);
		$resdata = $this->masterbank_model->getBankPerRecord();
		print json_encode($resdata[0]);
	}
	
	function simpan() 
	{
		/*$config = array();
		$config['per_page'] = 10;
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['curr_class'] = 'active';
		$config['full_tag_open'] = '<ul id="pagination-flickr">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['next_link'] = 'Next &gt;&gt;';
		$config['prev_link'] = '&lt;&lt; Previous';
		$config['next_tag_open'] = '<li class="next">';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li class="previous-off">';
		$config['prev_tag_close'] = '</li>';
		
		$config["uri_segment"] = 3;
		$config["base_url"] = site_url('kwitansipembeliancontroller/index');
		$config['total_rows'] = $this->kwitansipembelian_model->countGetList();
		
		$this->pagination_editable->initialize($config);
	
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;*/
		
        if (isset($_POST['btnSave'])) 
		{
			if (isset($_POST['keterangan']) && $_POST['keterangan'] !="" 
				&& isset($_POST['typebyr']) && $_POST['typebyr'] !="0"
				/*or isset($_POST['keterangangiro']) && $_POST['keterangangiro'] !=""*/)
			{
				
				$jumdata = count($this->input->post('chkitem'));
				//var_dump($this->input->post('chkitem'));exit;
				$i = 0;
				$ttlamtin=0;
				$ttlqty="";
				$stockstatus="BELI";
				$ttlamt="";
				$ttlfakqty = 0;
				$Qtyall = 0;
				$tglfaktur =date('Y-m-d', strtotime('now'));
				$tanggal=date('Y-m-d H:i:s', strtotime('now'));

				$kodenokwin = $this->kwitansipembelian_model->ambilnokwitansi();
				$this->kwitansipembelian_model->setNoKwitansi($kodenokwin);
				
				foreach($this->input->post('chkitem') as $iddelay)
				{			
					$kddtlbarang = $_POST['chkitem'][$i];
					$kdfakno	 = $_POST['kdidfaktur'][$i];
					$kdiddetail	 = $_POST['kdedetailid'][$i];
					$suppcode	 = $_POST['kdesupplier'][$i];
					$quanttity	 = $_POST['txtQuantity'][$i];
					$disct		 = 0;
					$amount		 = $_POST['txtAmount'][$i];
					$ttlqty		 = $ttlqty + $quanttity;
					$ttlamt		 = $ttlamt + $amount;
					
					// insert ke tabel transaksi Kwitansi
					$this->kwitansipembelian_model->setNoFaktur($kdfakno);
					$this->kwitansipembelian_model->setQuantity($quanttity);
					$this->kwitansipembelian_model->setAmount($amount);
					
					$this->kwitansipembelian_model->insertdetail();
	
					// update ke tabel faktur
					$this->transaksibankpembelian_model->setNoFaktur($kdfakno);
					$this->transaksibankpembelian_model->setIDdetail($kdiddetail);
					$Qtyall = $this->transaksibankpembelian_model->getTotalqty();
					
					if($Qtyall == $quanttity)
					{			
						$this->transaksibankpembelian_model->deletefakturpembelian();
					}
					else
					{
						$ttlfakqty = $Qtyall - $quanttity;
						$this->transaksibankpembelian_model->setTotalQuantity($ttlfakqty);
						$this->transaksibankpembelian_model->editquantheader();
						$this->transaksibankpembelian_model->deletefakturpembeliandetial();
					}
				
					// insert ke tabel stock dan transaksi stock
					$this->stock_model->setKodeSupplier($suppcode);
					$this->stock_model->setKodeDetailBarang($kddtlbarang);
					$stockArray['stok'] = $this->stock_model->cekkodestck();
					$stockkode = $this->stock_model->getStockListId();
					$stockAkhir = $this->stock_model->getStockCurrent();

					if($stockArray['stok'] != "0")
					{
						
						$this->stock_model->setKodeStok($stockkode[0]->kodestok);
						$latest_stock = intval($stockAkhir[0]->stockakhir) + intval($quanttity);
						$this->stock_model->setStokAkhir($latest_stock);
						$this->stock_model->setLastUpdateBy($this->session->userdata('userid'));
						$this->stock_model->setLastUpdateOn(date('Y-m-d'));
						$this->stock_model->update();
						
						$this->stock_model->setKodeStok($stockkode[0]->kodestok);
						$this->stock_model->setQuantity($quanttity);
						$this->stock_model->setStatus($stockstatus);
						$this->stock_model->setCreatedBy($this->session->userdata('userid'));
						$this->stock_model->setCreatedOn($tanggal);
						$this->stock_model->insertdetail();
					}
					else
					{
				
						$codestock = $this->stock_model->ambilkodestock();
						$this->stock_model->setKodeStok($codestock);
						$this->stock_model->setKodeSupplier($suppcode);
						$this->stock_model->setKodeDetailBarang($kddtlbarang);
						$this->stock_model->setStokAkhir($quanttity);
						$this->stock_model->setCreatedBy($this->session->userdata('userid'));
						$this->stock_model->setCreatedOn($tanggal);
						$this->stock_model->insert();	
						
						$this->stock_model->setKodeStok($codestock);
						$this->stock_model->setQuantity($quanttity);
						$this->stock_model->setStatus($stockstatus);
						$this->stock_model->setCreatedBy($this->session->userdata('userid'));
						$this->stock_model->setCreatedOn($tanggal);
						$this->stock_model->insertdetail();
					}
					
					$i++;
				}
				
				if ($_POST['typebyr']=="CASH")
				{	
					// insert ke tabel Cash
					$this->kwitansimutasicash_model->setNoKwitansi($kodenokwin);
					$this->kwitansimutasicash_model->setNoFaktur("-");
					$this->kwitansimutasicash_model->setTotalQuantity($ttlqty);
					$this->kwitansimutasicash_model->setDiscount("0");
					$this->kwitansimutasicash_model->setTotalAmount_In($ttlamtin);
					$this->kwitansimutasicash_model->setTotalAmount_Out($_POST['ttlamount']);
					$this->kwitansimutasicash_model->setKeterangan($_POST['keterangan']);
					$this->kwitansimutasicash_model->setCreatedBy($this->session->userdata('userid'));
					$this->kwitansimutasicash_model->setCreatedOn($tanggal);
					$this->kwitansimutasicash_model->setActive('1');
					
					$this->kwitansimutasicash_model->insert();
				}
				elseif ($_POST['typebyr']=="BANK")
				{
					// insert ke tabel mutasi bank
					$this->kwitansimutasibank_model->setNoKwitansi($kodenokwin);
					$this->kwitansimutasibank_model->setNoFaktur("-");
					$this->kwitansimutasibank_model->setTotalQuantity($ttlqty);
					$this->kwitansimutasibank_model->setDiscount("0");
					$this->kwitansimutasibank_model->setTotalAmount_In($ttlamtin);
					$this->kwitansimutasibank_model->setTotalAmount_Out($_POST['ttlamountbank']);
					$this->kwitansimutasibank_model->setKodeBank($_POST['bank']);
					$this->kwitansimutasibank_model->setKeterangan($_POST['keterangan']);
					$this->kwitansimutasibank_model->setCreatedBy($this->session->userdata('userid'));
					$this->kwitansimutasibank_model->setCreatedOn($tanggal);
					$this->kwitansimutasibank_model->setActive('1');
					
					$this->kwitansimutasibank_model->insert();
				}
				elseif ($_POST['typebyr']=="GIRO")
				{
					// insert ke tabel mutasi Giro
					
					$this->kwitansimutasigiro_model->setNoKwitansi($kodenokwin);
					$this->kwitansimutasigiro_model->setNoFaktur("-");
					$this->kwitansimutasigiro_model->setTotalQuantity($ttlqty);
					$this->kwitansimutasigiro_model->setTotalAmount_In($ttlamtin);
					$this->kwitansimutasigiro_model->setTotalAmount_Out($_POST['ttlamountgiro']);
					$this->kwitansimutasigiro_model->setBankname($_POST['nmbankgiro']);
					$this->kwitansimutasigiro_model->setNogiro($_POST['nogiro']);
					$this->kwitansimutasigiro_model->setTanggalterbit($_POST['tglterbit']);
					$this->kwitansimutasigiro_model->setTanggalcair($_POST['tglcair']);
					$this->kwitansimutasigiro_model->setKeterangan($_POST['keterangan']);
					$this->kwitansimutasigiro_model->setCreatedBy($this->session->userdata('userid'));
					$this->kwitansimutasigiro_model->setCreatedOn($tanggal);
					$this->kwitansimutasigiro_model->setActive('1');
					
					$this->kwitansimutasigiro_model->insert();
				}
								
				// insert ke tabel kwitansi
				$this->kwitansipembelian_model->setTotalQuantity($ttlqty);
				$this->kwitansipembelian_model->setTotalAmount($ttlamt);
				$this->kwitansipembelian_model->setTypebayar($_POST['typebyr']);
				if ($_POST['typebyr']=="CASH")
				{
					$this->kwitansipembelian_model->setPay_amount($_POST['ttlamount']);
					$this->kwitansipembelian_model->setKeterangan($_POST['keterangan']);
				}
				elseif ($_POST['typebyr']=="BANK")
				{
					// update ke tabel Account bank
					$this->mstraccount_model->setKodeBank($_POST['bank']);
					
					$stockArray = $this->mstraccount_model->gettotalamount();
					
					$saldoawl = $stockArray[0]->SaldoAwal;
					$uangmsk = $stockArray[0]->Uangmasuk;
					$uangout = $stockArray[0]->Uangkeluar;
					$sldakhr = $stockArray[0]->SaldoAkhir;
					
					$ttlsldawal = $saldoawl;
					$ttluangmsk = $uangmsk;
					$ttluangout = $uangout + $_POST['ttlamountbank'];
					$ttlsldakhr = $sldakhr - $_POST['ttlamountbank'];
					
					$this->mstraccount_model->setSaldoAwal($ttlsldawal);
					$this->mstraccount_model->setUangmasuk($ttluangmsk);
					$this->mstraccount_model->setUangkeluar($ttluangout);
					$this->mstraccount_model->setSaldoAkhir($ttlsldakhr);
					$this->mstraccount_model->setLastupdateby($this->session->userdata('userid'));
					$this->mstraccount_model->setLastUpdateOn($tanggal);
					
					$this->mstraccount_model->ubahmstraccount();
					// sampe sini
					
					$this->kwitansipembelian_model->setPay_amount($_POST['ttlamountbank']);
					
					$this->kwitansipembelian_model->setKeterangan($_POST['keterangan']);
				}
				elseif ($_POST['typebyr']=="GIRO")
				{
					$this->kwitansipembelian_model->setPay_amount($_POST['ttlamountgiro']);
					$this->kwitansipembelian_model->setKeterangan($_POST['keterangan']);
				}

				$this->kwitansipembelian_model->setCreatedBy($this->session->userdata('userid'));
				$this->kwitansipembelian_model->setCreatedOn($tanggal);
				$this->kwitansipembelian_model->setLastUpdateBy($this->session->userdata('userid'));
				$this->kwitansipembelian_model->setLastUpdateOn($tanggal);
				$this->kwitansipembelian_model->setActive('1');
				$this->kwitansipembelian_model->insert();				
				
				$dataprofile = array(
					'page_title' => 'Finance > Pembayaran Pembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodekwitansino'=> $this->kwitansipembelian_model->ambilnokwitansi1(),
					'kodesuppkwitansi'=> $this->kwitansipembelian_model->ambilsupplier(),
					'transkwitansi' => $this->kwitansipembelian_model->getListfakturkwt()	
				);		
				$content = array(
					'content' => 'finance/kwitansi/pembelian/formkwitansipembelian'
				);
				$this->template->load('templates/default',$content,$dataprofile);
				
				//redirect('kwitansipembeliancontroller');
			}
			else
			{
				$dataprofile = array(
					'page_title' => 'Finance > kwitansi Pembelian',
					'name' => $this->session->userdata('fullname'),
					//'mstrfaktur' => $this->kwitansipembelian_model->ambilnofaktur(),
					'masterbank' => $this->masterbank_model->getListbank(),
					'bankcombo' => $this->bankCombobox(),
					'message' => '<div style="color:red;font-size:9pt;font-weight:bold;">Keterangan Belum di isi </div>',
					'det_barang' => $this->kwitansipembelian_model->getList(),
					
					//'links' => $this->pagination_editable->create_links()
                );
                $content = array(
                    'content' => 'finance/kwitansi/pembelian/kwitansipembelian'
                );
                $this->template->load('templates/default', $content, $dataprofile);
			}

        } 
		else 
		{
            $dataprofile = array(
				'page_title' => 'Finance > kwitansi Pembelian',
				'name' => $this->session->userdata('fullname'),
				//'mstrfaktur' => $this->kwitansipembelian_model->ambilnofaktur(),
				'masterbank' => $this->masterbank_model->getListbank(),
				'bankcombo' => $this->bankCombobox(),
				'det_barang' => $this->kwitansipembelian_model->getList(),
				//'links' => $this->pagination_editable->create_links(),
				'message'=>''	
			);	
			
			$content = array(
				'content' => 'finance/kwitansi/pembelian/kwitansipembelian'
			);
			$this->template->load('templates/default',$content,$dataprofile);		
		}
    }
}